package com.fangwei.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fangwei.pojo.ErrorInfo;
import com.fangwei.pojo.MyException;

/**
 * 全局的异常处理类
 * @author root
 *
 */
@ControllerAdvice
public class GlobalExceptionHandler {

	public static final String DEFAULT_ERROR_VIEW ="error";
	
	@ExceptionHandler(value=Exception.class)
	@ResponseBody
	public ErrorInfo<String> defaultErrorHandler(HttpServletRequest request,MyException e) throws Exception{
		ErrorInfo<String> r = new ErrorInfo<String>();
		r.setMessage(e.getMessage());
		r.setCode(ErrorInfo.ERROR);
		r.setData("SomeData");
		r.setUrl(request.getRequestURL().toString());
		return r;
	}
//	@ExceptionHandler(value=Exception.class)
//	public ModelAndView defaultErrorHandler(HttpServletRequest request,Exception e) throws Exception{
//		ModelAndView mv = new ModelAndView();
//		mv.addObject("e", e);
//		mv.addObject("url", request.getRequestURL());
//		mv.setViewName(DEFAULT_ERROR_VIEW);
//		return mv;
//	}
}
